<?php

	$dbName = "usr_web12_16";
	$dbHost = "localhost";
	$dbUser = "web12";
	$dbPassword ="v2S7YfqT";

	function insertScore($scoreItem) {
	
		$con = mysql_connect($GLOBALS["dbHost"],$GLOBALS["dbUser"],$GLOBALS["dbPassword"]);	
		mysql_select_db($GLOBALS["dbName"], $con);	

		if (!$con)
            return array('error' => "Could not connect: " . mysql_error(), 'params' => var_export($params, TRUE));
		
		$query = "INSERT INTO Score(Name, Score) VALUES('". $scoreItem['Name']."', '" . $scoreItem['Score'] . "')";
		$result = mysql_query($query, $con);
	
		if (!$result) 
            return array('error' => "Could not successfully run query ($sql) from DB: " . mysql_error(), 'params' => var_export($params, TRUE));

		error_log(print_r($query, true), 3, 'debug.log'); 

		mysql_close($con);	
		return $result[0];	
	}
	
	function getScore($limit) {
	
		$con = mysql_connect($GLOBALS["dbHost"],$GLOBALS["dbUser"],$GLOBALS["dbPassword"]);	
		mysql_select_db($GLOBALS["dbName"], $con);

		if (!$con)
            return array('error' => "Could not connect: " . mysql_error(), 'params' => var_export($params, TRUE));
		
		$query = "SELECT Name, Score FROM Score ORDER BY Score DESC LIMIT " . array_shift($limit);
		$result = mysql_query($query, $con);
	
		if (!$result) 
            return array('error' => "Could not successfully run query ($sql) from DB: " . mysql_error(), 'params' => var_export($params, TRUE));

		$res_array = array();
		
		$count = 0;
		while($row = mysql_fetch_assoc($result))
		{
			$res_array[$count] = $row;
			$count++;
		} 

		error_log(print_r($query, true), 3, 'debug.log'); 

		mysql_close($con);	
		return $res_array;	
	}
	
	function getWorlds($limit) {
	
		$con = mysql_connect($GLOBALS["dbHost"],$GLOBALS["dbUser"],$GLOBALS["dbPassword"]);	
		mysql_select_db($GLOBALS["dbName"], $con);

		if (!$con)
            return array('error' => "Could not connect: " . mysql_error(), 'params' => var_export($params, TRUE));
		
		$query = "SELECT Id, Genre, Scale FROM Worlds";
		$result = mysql_query($query, $con);
	
		if (!$result) 
            return array('error' => "Could not successfully run query ($sql) from DB: " . mysql_error(), 'params' => var_export($params, TRUE));

		$res_array = array();
		
		$count = 0;
		while($row = mysql_fetch_assoc($result))
		{
			$res_array[$count] = $row;
			$count++;
		} 

		error_log(print_r($query, true), 3, 'debug.log'); 

		mysql_close($con);	
		return $res_array;	
	}
	
	function getLevelCount() {
	
		return executeStoredProcedure('getLevelCount()');
	}
	
	function getLevel($id){
		
		$query = "getLevel(" . array_shift($id) . ");";
		return executeStoredProcedure($query);		
	} 
	
	//PHP MySQLi instead php 5.2.2 (bug)
	function executeStoredProcedure($query) {
		
		$mysqli = new MySQLi($GLOBALS["dbHost"], $GLOBALS["dbUser"], $GLOBALS["dbPassword"], $GLOBALS["dbName"]);
		$query = 'CALL ' . $query;
		$success = $mysqli->real_query($query) or print($mysqli->error);
		
		if (!$success)
            return array('error' => "Could not connect: " . mysql_error(), 'params' => var_export($params, TRUE));
			
		$result = $mysqli->store_result();	
		return $result->fetch_assoc();
	}
?>